﻿using up6.db.model;
using up6.db.store;
using up6.utils;

namespace up6.store.minio;

public class MinioWriter : FileBlockWriter
{
    public MinioWriter()
    {
        this.storage = StorageType.Minio;
    }

    /**
     * 生成UploadId
     */
    public override async Task<string> makeAsync(FileInf file)
    {
        return await MinioTool.CreateMultipartUploadAsync(file.S3Key());
    }

    public override async Task<string> writeAsync(FileInf file, Stream stm)
    {
        byte[] data = new byte[stm.Length];
        stm.Seek(0, SeekOrigin.Begin);
        stm.Read(data, 0, data.Length);
        stm.Close();
        stm = null;

        var etag = await MinioTool.UploadPartAsync(file.S3Key(), file.blockIndex, file.object_id, data);
        file.etag = etag;
        file.saveEtags();
        return etag;
    }

    public override async Task<bool> writeLastPartAsync(FileInf file)
    {
        return await MinioTool.CompleteMultipartUploadAsync(file);
    }
}